﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using DevExpress.Web.ASPxTreeList;

public partial class UserControl_ControlReportThongTinTuyenDuong : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = webapp4U.Configuration.CurrentConnectionString;
    string error = "";
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            //if (Application["db"] != null)
            //{
            //    DataTable db = (DataTable)Application["db"];
            //    ASPxPivotGrid1.DataSource = db;
            //    ASPxPivotGrid1.DataBind();
            //}
        }
       
    }
    protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
    {
        ASPxTreeList1.ExpandAll();
        //dt = new ClassExecute.ClassExecute();
        //try
        //{
        //    DataTable db = dt.Get_Data_DataTable(str, ref error, "[k_reportThongTinTuyenDuong]", CommandType.StoredProcedure);
        //    if (db.Rows.Count > 0)
        //    {

        //        ASPxPivotGrid1.DataSource = db;
        //        Application["db"] = db;
        //    }
        //    else
        //    {
        //        if (Application["db"] != null)
        //        {
        //            Application["db"] = null;
        //        }
        //        ASPxPivotGrid1.DataSource = null;
        //    }
        //    ASPxPivotGrid1.DataBind();
        //}
        //catch (Exception ex)
        //{

        //    string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
        //    dt.WriteErrorLog(Server.MapPath(webapp4U.Globals.LogUrl()), ex.Message, method);
        //}
    }
    protected void imgBtnSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            ASPxTreeList1.ExpandAll();
            string filename =DateTime.Now.Year.ToString() + "_" + DateTime.Now.Month.ToString() + "_" + DateTime.Now.Day.ToString() + "_" +  "PhanTuyen";
            ASPxTreeListExporter1.WriteXlsToResponse(filename);
        }
        catch 
        {
           
        }
    }
   
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // ArrangeReport();
    }
    protected void ASPxTreeList1_VirtualModeCreateChildren(object sender, TreeListVirtualModeCreateChildrenEventArgs e)
    {
        DataTable db = new DataTable();
        if (ViewState["db"] == null)
        {
            dt = new ClassExecute.ClassExecute();
            db = dt.Get_Data_DataTable(str, ref error, "k_reportThongTinTuyenDuong", CommandType.StoredProcedure);
            db.PrimaryKey = new DataColumn[] { db.Columns["salesp_key"] };
            ViewState["db"] = db;
            
        }
        else
        {

            db = (DataTable)ViewState["db"];

        }


        DataRow row = e.NodeObject as DataRow;
        if (row == null)
        {
            if ((Session["UserType"].ToString()=="tdv")||(Session["UserType"].ToString()=="asm")) 
            {
                e.Children = db.Select("salesp_key='" + Session["user"].ToString() + "'");
            }
            else
            {
                //string salesp_key = "FM";
                e.Children = db.Select("salesp_key='" + db.Rows[0][0].ToString() + "'");
            }
        }
        else
        {
            //e.Children = db.Select(string.Format("Recommender={0}",Convert.ToString(row["Salesp_Key"])));

            e.Children = db.Select("Recommender='" + row["salesp_key"] + "'");

        }
        //ASPxTreeList1.ExpandAll();
    }
    protected void ASPxTreeList1_VirtualModeNodeCreating(object sender, TreeListVirtualModeNodeCreatingEventArgs e)
    {
        //string nodePath = e.NodeObject.ToString();
        //e.NodeKeyValue = GetNodeGuid(nodePath);
        //e.IsLeaf = !Directory.Exists(nodePath);
        //e.SetNodeValue("name", Path.GetFileName(nodePath));
        //e.SetNodeValue("date", Directory.GetCreationTime(nodePath));
        //DataTable myTable = Application["topicTable"] as DataTable;

        DataTable myTable = ViewState["db"] as DataTable;
        DataRow row = e.NodeObject as DataRow;
        
        if (row == null)
        {
            return;
        }
        else
        {
            e.NodeKeyValue = row["salesp_key"];

            e.SetNodeValue("salesp_key", row["salesp_key"]);
            e.SetNodeValue("Recommender", row["Recommender"]);
            e.SetNodeValue("salesp_name", row["salesp_name"]);
            e.SetNodeValue("CapBac", row["CapBac"]);
            e.SetNodeValue("Active", row["Active"]); 

        }
        DataTable db = (DataTable)ViewState["db"];


    }
   
}